import 'package:flutter/material.dart';
import 'package:flutter_base/src/utils/navigation_service.dart';
import 'package:fluttertoast/fluttertoast.dart';

class ToastUtil {
  static bool _isToastShowing = false; // 用于控制 Toast 是否已显示

  static void show(String msg, {int duration = 1}) async {
    /// 隐藏键盘，防止键盘遮挡 Toast
    FocusScope.of(NavigationService.context).requestFocus(FocusNode());
    Fluttertoast.showToast(
      msg: msg,
      toastLength: Toast.LENGTH_LONG,
      gravity: ToastGravity.TOP,
      timeInSecForIosWeb: duration,
      fontSize: 16,
    );
  }

  static void onlyOneShow(String msg, {int duration = 1}) {
    FocusScope.of(NavigationService.context).requestFocus(FocusNode());
    if (!_isToastShowing) {
      // 检查当前是否有 Toast 显示
      _isToastShowing = true; // 标记为正在显示

      show(msg);

      // 设置一个定时器，duration 秒后重置标志
      Future.delayed(Duration(seconds: duration), () {
        _isToastShowing = false; // 重置标志
      });
    }
  }
}
